<template>
  <div>

    <el-row>
      <el-col :span="3" :offset="20" style="height: 60px; line-height: 60px;">
        <el-button size="small" @click="village = {institutionId: selectedInstitution.id}, showAddVillage = true">添加村庄</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="villageList"
      element-loading-text="Loading"
      border
      fit
    >

      <el-table-column align="center" label="村庄名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="管理区">
        <template slot-scope="scope">
          {{ scope.row.institutionName }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="village = scope.row; showUpdateVillage = true">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteVillage(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div style="text-align: center; margin-top: 30px;">
      <el-button @click="onCancel">关闭</el-button>
    </div>

    <el-dialog
      title="添加村庄"
      :visible.sync="showAddVillage"
      append-to-body>

      <el-form ref="ruleForm" :rules="rules" :model="village" label-width="120px">

        <el-form-item label="村庄名称" prop="name">
          <el-input v-model="village.name" />
        </el-form-item>

        <el-form-item style="text-align: center;">
          <el-button type="primary" @click="addVillage('ruleForm')">添加</el-button>
          <el-button @click="showAddVillage = false">关闭</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

    <el-dialog
      title="修改村庄"
      :visible.sync="showUpdateVillage"
      append-to-body>

      <el-form ref="ruleForm" :rules="rules" :model="village" label-width="120px">

        <el-form-item label="村庄名称" prop="name">
          <el-input v-model="village.name" />
        </el-form-item>

        <el-form-item style="text-align: center;">
          <el-button type="primary" @click="updateVillage('ruleForm')">修改</el-button>
          <el-button @click="showUpdateVillage = false">关闭</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs';
export default {
  props: ['selectedInstitution'],
  data() {
    return {
      showAddVillage: false,
      showUpdateVillage: false,
      listLoading: false,
      village: {},
      villageList: [],
      rules: {
        name: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.village.institutionId = this.selectedInstitution.id;
    this.getVillageListByInstitutionId();
  },
  methods: {
    addVillage(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          request({
            url: '/api/village/addVillage',
            method: 'post',
            data: this.village
          }).then((res) => {
            this.$message(res.resultCodeDesc);
            if (res.resultCode === '200') {
              this.getVillageListByInstitutionId();
              this.showAddVillage = false;
            }
          }).catch(() => {

          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    updateVillage(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          request({
            url: '/api/village/updateVillage',
            method: 'post',
            data: this.village
          }).then((res) => {
            this.$message(res.resultCodeDesc);            
            if (res.resultCode === '200') {
             this.getVillageListByInstitutionId();
             this.showUpdateVillage = false;
            }
          }).catch(() => {

          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    deleteVillage(index, row) {
      this.$confirm('确认删除？')
        .then(_ => {
          request({
            url: '/api/village/deleteVillage',
            method: 'post',
            data: qs.stringify({ id: row.id }),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
          }).then((res) => {
            this.$message(res.resultCodeDesc);
            this.getVillageListByInstitutionId();
          }).catch(_ => {})
        })
        .catch(_ => {});
    },
    getVillageListByInstitutionId() {
      this.listLoading = true;
      request({
        url: '/api/village/getVillageListByInstitutionId',
        method: 'get',
        params: { institutionId: this.selectedInstitution.id }
      }).then((res) => {
        this.villageList = res.result;
        this.listLoading = false;
      }).catch(() => {

      })
    },
    onCancel() {
      this.$emit('closeDialog', false);
    }
  }
}
</script>

<style scoped>

</style>
